__all__ = ['DatavHandler']

# coding:utf-8
from flask_restful import Resource
from flask_restful import reqparse

from app import cache
from app.datav.utils.monitor import Monitor


class DatavHandler(Resource):

    def __init__(self):
        self.choices = ['japi_qpm', 'req_time', 'applied_notification', 'product_code', 'map_users', 'map_applied',
                        'japi_jvm_threads_busy', 'rds_pg_cpu_usage', 'rds_pg_mem_usage']
        self.get_parser = reqparse.RequestParser()
        self.get_parser.add_argument('item', type=str, choices=self.choices, required=True, trim=True, location='args',
                                     help='item: {error_msg}')
        self.res = None

    def get(self):
        args = self.get_parser.parse_args()
        item = args.get('item')
        monitor = Monitor()
        if hasattr(monitor, item):
            func = getattr(monitor, item)
            self.res = func(item)
            print(self.res)

        if not self.res:
            cache.delete(item)
            return {"status": 0, "msg": "failed to get interface data", "data": {}}, 400

        return {"status": 1, "msg": "successful", "data": self.res}, 200
